package com.yx.cloud.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.viewpager.widget.ViewPager;

public class MusicViewPager extends ViewPager {

    private float xDistance, yDistance, xLast, yLast;
    public MusicViewPager(Context context) {
        super(context);
    }

    public MusicViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    /**
     * 横向滑动时ViewPager拦截事件，处理viewpager嵌套listview滑动冲突，增加逻辑 xDistance > 10，防止点击事件不生效
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                super.onInterceptTouchEvent(ev);
                return false;//必须要在down事件return false，否则listView就接收不到down事件，也就无法处理touchevent
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance && xDistance > 10) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
}

